package com.mathformulas.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.mathformulas.R;
import com.mathformulas.viewmodel.NavDrawerItem;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NavDrawerExpandableListAdapter extends BaseExpandableListAdapter {

	private Context context;
	private ArrayList<NavDrawerItem> headerItems;
	// child data in format of header title, child title
	private HashMap<String, String[]> childItems = new HashMap<String, String[]>();

	public NavDrawerExpandableListAdapter(Context context,
			ArrayList<NavDrawerItem> navDrawerHeaderItems,
			HashMap<String, String[]> listDataChild) {
		super();
		this.context = context;
		this.headerItems = navDrawerHeaderItems;
		this.childItems = listDataChild;
	}

	public NavDrawerExpandableListAdapter(Context context,
			ArrayList<NavDrawerItem> navDrawerHeaderItems) {
		super();
		this.context = context;
		this.headerItems = navDrawerHeaderItems;
	}

	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		return this.childItems.get(this.headerItems.get(groupPosition)
				.getTitle())[childPosititon];
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) context
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.drawer_list_child, null);
		}

		TextView tvChildTile = (TextView) convertView
				.findViewById(R.id.tvChildTile);

		final String childText = (String) getChild(groupPosition, childPosition);
		tvChildTile.setText(childText);

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return this.childItems.get(this.headerItems.get(groupPosition)
				.getTitle()).length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return this.headerItems.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return this.headerItems.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) this.context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.drawer_list_header,
					null);
		}
		NavDrawerItem header = (NavDrawerItem) getGroup(groupPosition);

		ImageView imgIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
		TextView txtTitle = (TextView) convertView.findViewById(R.id.tvTitle);

		imgIcon.setImageResource(header.getIcon());
		txtTitle.setText(header.getTitle());

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}
